<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-05-17
 * Time: 18:54
 */

class IndexModel extends MY_Model{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 帖子group by day
     * @param $startTime
     * @param $endTime
     * @return array
     * @throws Exception
     */
    public function tongjiPostDay($startTime, $endTime){
        $sql = "select add_time from ( select date_format(FROM_UNIXTIME(add_time),'%Y-%m-%d') as add_time from t_post ";
        $sql.= " where add_time >=$startTime and add_time <=$endTime ";
        $sql.= " ) as a GROUP BY a.add_time order by add_time desc";
        $dayList = $this->query_sql_list($sql);
        return $dayList;
    }
    public function whereSql($where){
        $sql = "";
        if(isset($where["startTime"])){
            $sql.= " and add_time >=".$where["startTime"];
        }
        if(isset($where["endTime"])){
            $sql.= " and add_time <=".$where["endTime"];
        }
        if(isset($where["is_audit"])){
            $sql.= " and is_audit =".$where["is_audit"];
        }
        if(isset($where["status"])){
            $sql.= " and status =".$where["status"];
        }
        if(isset($where["is_hot"])){
            $sql.= " and is_hot =".$where["is_hot"];
        }
        if(isset($where["source_type"])){
            if($where["source_type"] == 1){ //机器人
                $sql.= " and source !=''";
            }else if($where["source_type"] == 2){
                $sql.= " and source =''";
            }
        }
        return $sql;
    }
    /**
     * 获取帖子统计数量
     * @param array $where
     * @return int
     * @throws Exception
     */
    public function tongjiPostNum($where = array()){
        $sql = "select count(*) as num from t_post where 1=1 ";
        $sql.= $this->whereSql($where);
        return $this->query_sql_count($sql);
    }

    /**
     * 评论group by day
     * @param $startTime
     * @param $endTime
     * @return array
     * @throws Exception
     */
    public function tongjiReplyDay($startTime, $endTime){
        $sql = "select add_time from ( select date_format(FROM_UNIXTIME(add_time),'%Y-%m-%d') as add_time from t_comment ";
        $sql.= " where add_time >=$startTime and add_time <=$endTime ";
        $sql.= " ) as a GROUP BY a.add_time order by add_time desc";
        $dayList = $this->query_sql_list($sql);
        return $dayList;
    }
    public function whereReplySql($where){
        $sql = "";
        if(isset($where["startTime"])){
            $sql.= " and add_time >=".$where["startTime"];
        }
        if(isset($where["endTime"])){
            $sql.= " and add_time <=".$where["endTime"];
        }
        if(isset($where["is_audit"])){
            $sql.= " and is_audit =".$where["is_audit"];
        }
        if(isset($where["status"])){
            $sql.= " and status =".$where["status"];
        }
        if(isset($where["source_type"])){
            if($where["source_type"] == 1){ //机器人
                $sql.= " and source !=''";
            }else if($where["source_type"] == 2){
                $sql.= " and source =''";
            }
        }
        return $sql;
    }
    /**
     * 获取帖子统计数量
     * @param array $where
     * @return int
     * @throws Exception
     */
    public function tongjiReplyNum($where = array()){
        $sql = "select count(*) as num from t_comment where 1=1 ";
        $sql.= $this->whereReplySql($where);
        return $this->query_sql_count($sql);
    }

    /**
     * 用户数据统计
     * @param $startTime
     * @param $endTime
     * @param int $type
     * @param string $user_type
     * @return int
     * @throws Exception
     */
    public function tongjiUserNum($startTime, $endTime, $type = 1, $user_type =""){
        if($type == 1){ //注册用户数
            $sql = "select count(*) as num from t_user where regdate >=".$startTime." and regdate <=".$endTime;
            if($user_type == 1){ //机器人
                $sql.= " and user_type in (2,3)";
            }else if($user_type == 2){ //注册用户
                $sql.= " and user_type in (1,4)";
            }
        }else if($type == 2){ //登录用户数
            $sql = "select count(*) as num from t_user where last_login >=".$startTime." and last_login <=".$endTime;
            if($user_type == 1){ //机器人
                $sql.= " and user_type in (2,3)";
            }else if($user_type == 2){ //注册用户
                $sql.= " and user_type in (1,4)";
            }
        }else if($type == 3){ //发主贴用户
            $sql = "select count(*) as num from t_post where add_time >=".$startTime." and add_time <=".$endTime;
            if($user_type == 1){ //机器人
                $sql.= " and source !=''";
            }else if($user_type == 2){ //注册用户
                $sql.= " and source=''";
            }
        }else if($type == 4){ //发回复用户
            $sql = "select count(*) as num from t_comment where add_time >=".$startTime." and add_time <=".$endTime;
            if($user_type == 1){ //机器人
                $sql.= " and source !=''";
            }else if($user_type == 2){ //注册用户
                $sql.= " and source=''";
            }
        }else if($type == 5){ //点赞用户
            $sql = "select count(*) as num from t_like where is_del=0 and add_time >=".$startTime." and add_time <=".$endTime;
            if($user_type == 1){ //机器人
                $sql.= " and u_type=1";
            }else if($user_type == 2){ //注册用户
                $sql.= " and u_type=0";
            }
        }else if($type == 6){ //分享用户
            $sql = "select count(*) as num from t_user_share where add_time >=".$startTime." and add_time <=".$endTime;
        }
        return $this->query_sql_count($sql);
    }
}